﻿from HttpTestcase.serializers import TestcaseReportsSerializer
from apps.HttpTestcase.models import TestcaseReports
from apps.HttpTestcase.serializers import ReportsSerializer
from apps.HttpTestcase.filters import Test_reportsFilter
from rest_framework.filters import OrderingFilter
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.generics import DestroyAPIView
from utils.viewsets import *
from utils.response import SuccessResponse
from rest_framework.views import APIView
from django.core import serializers
import json


class ReportViewSet(ModelViewSet):
    queryset = TestcaseReports.objects.filter(is_delete=False, is_testsuite=False)
    serializer_class = ReportsSerializer
    # 过滤
    filter_class = Test_reportsFilter
    # filter_fields = ['id']
    # 指定权限类
    # permission_classes = [permissions.IsAuthenticated]
    perms_map = {'get': '*', 'post': 'case_reports_create',
                 'put': 'case_reports_update', 'delete': 'case_reports_delete'}
    # 排序
    filter_backends = [DjangoFilterBackend, OrderingFilter]
    filter_fields = ['execute_time', 'version']
    ordering_fields = ['execute_time', 'version']


class Report_suiteAPIView(APIView):
    def get(self, request, *args, **kwargs):
        data = []
        if request.query_params.get('testsuite_reports'):
            # 如果testsuite_reports存在，说明是场景详情页下查询用例列表
            testsuite_reports_id = request.query_params.get('testsuite_reports')
            testcase_repost_all = TestcaseReports.objects.filter(
                testsuite_reports_id=testsuite_reports_id)
            for item in testcase_repost_all:
                dict = {
                    'testcase_name': item.testcase_name,
                    'id': item.id,
                    'project_name': item.project_name,
                    'api_name': item.api_name,
                    'url': item.url,
                    'execute_time': item.execute_time,
                    'actual_status_code': item.actual_status_code,
                    'elapsed_ms': item.elapsed_ms,
                    'actual_response_data': json.dumps(item.actual_response_data),
                    'failure_reason': item.failure_reason,
                    'status': item.status,

                }
                data.append(dict)
        elif request.query_params.get('testcase_id'):
            # 如果testcase_id存在，说明是场景报告详情下用例详情跳转
            testcase_id = request.query_params.get('testcase_id')
            testcase_repost = TestcaseReports.objects.filter(id=testcase_id)
            for item in testcase_repost:
                dict = {
                    'testsuite': item.testsuite_reports.testsuite_name,
                    'testcase_name': item.testcase_name,
                    'project_name': item.testcase_name,
                    'api_name': item.api_name,
                    'url': item.url,
                    'method': item.method,
                    'version': item.version,
                    'execute_time': item.execute_time,
                    'status': item.status,
                    'headers': item.headers,
                    'request_data': item.request_data,
                    'actual_response_data': item.actual_response_data,
                    'failure_reason': item.failure_reason,
                    'extract_value': item.extract_value,
                }
                # data.append(dict)
                return SuccessResponse(data={'data': dict}, msg="查询成功！")
        return SuccessResponse(data={'data': data}, msg="查询成功！")


class TestReportsDelete(DestroyAPIView):
    perms_map = {'delete': 'case_reports_delete'}

    def delete(self, request, *args, **kwargs):
        data = request.data
        testlist = data['id']
        testlist_id = []
        for item in testlist:
            testlist_id.append(item.get('id'))

        if data.get('is_de'):
            TestcaseReports.objects.filter(id__in=testlist_id).delete()
        else:
            TestcaseReports.objects.filter(id__in=testlist_id).update(is_delete=True)

        return SuccessResponse(data=None, msg="删除成功！")
